package com.test.test11.task;

import android.os.AsyncTask;
import android.util.Log;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.test.test11.SharedConstants;
import com.test.test11.WebURL;
import com.test.test11.entity.ResponseResult;
import com.test.test11.entity.UserDeviceRelationship;
import com.test.test11.http.AfterRequest;
import com.test.test11.http.HttpClient;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 从服务器获取设备列表
 */
//@JsonIgnoreProperties(ignoreUnknown = true)
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
public class DeviceListGetTask extends AsyncTask<Void, Void, ResponseResult>{

    private static final String TAG = "DEVICE_LIST_TASK";

    private AfterRequest afterRequest;

    public DeviceListGetTask(AfterRequest afterRequest){
        this.afterRequest = afterRequest;
    }

    /**
     * 后台获取进程
     * @param voids
     * @return
     */
    @Override

    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
    protected ResponseResult doInBackground(Void... voids) {

        Map<String, Object> params = new HashMap<>();
      //  params.put("id", SharedConstants.CURRENT_USER.getId());
        params.put("id", "39bcb9d1086f4b039ca9424b86ebdb8b");

        try {
            String res = HttpClient.getInstance().post(WebURL.DEVICE_LIST_GET_URL, params);
            if(StringUtils.isBlank(res)){
                return null;
            }
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            ResponseResult result = mapper.readValue(res, ResponseResult.class);
            if(result.getCode()==0){
                System.out.println("8888888888888");
                TypeFactory typeFactory = mapper.getTypeFactory();
                CollectionType collectionType = typeFactory.constructCollectionType(List.class, UserDeviceRelationship.class);

                JsonFactory factory = new JsonFactory();
                System.out.println("8888888888888999999");
                String userDeviceStr = mapper.writeValueAsString(result.getObject());
                Log.i(TAG, userDeviceStr);
                System.out.println("88888999999999999");
                JsonParser parser = factory.createParser(userDeviceStr);
                System.out.println("最新的"+parser);
                List<UserDeviceRelationship> list = mapper.readValue(parser, collectionType);
                System.out.println("8888888888888llll");

                if(list!=null && list.size()>0){
                    SharedConstants.setDeviceList(list);
                    System.out.println("8888888888888999");
                }else{
                    System.out.println("8888888888888999pppp");
                    SharedConstants.setDeviceList(new ArrayList<UserDeviceRelationship>());
                }
            }
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
